package pl.edu.agh.portal.application.portlets;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import pl.edu.agh.portal.application.utils.PortletsUtils;
import pl.edu.agh.thesis.webservices.task.client.TaskManagerStub;
import pl.edu.agh.thesis.webservices.task.client.TaskUtils;

public class TaskEditPortlet extends ThesisGenericPortlet {

	private static final String NORMAL_VIEW = "/task_edit_portlet.jsp";
	
	private PortletRequestDispatcher normalView;
	
	private Map<String, String> errorList = new HashMap<String, String>();	
	private TaskManagerStub.Task task;	
	
	public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
				
		String taskIdString = (String)PortletsUtils.vault.get("taskId");
		int taskId = Integer.parseInt(taskIdString);
				
		task = TaskUtils.get(PortletsUtils.temp_session, taskId);
		
		request.setAttribute(PortletsUtils.ATTRIBUTE_TASK_FOR_EDITION, task);
		request.setAttribute(PortletsUtils.ATTRIBUTE_ERROR_MESSAGES, errorList);
						
		normalView.include(request, response);		
	}
	
	@Override
	public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {
		
		String value = request.getParameter(PortletsUtils.TASK_EDITION_KEY);		
		
		if (value.equals(PortletsUtils.BUTTON_CANCEL)) {
			response.sendRedirect("/portal/portal/task_details.psml");
		} else if (value.equals(PortletsUtils.BUTTON_SAVE)) {
			validate(request);
			if (errorList.size() == 0) {				
				TaskUtils.modify(PortletsUtils.temp_session, task);
				response.sendRedirect("/portal/portal/task_details.psml");
			}			
		} else {
			response.setPortletMode(PortletMode.VIEW);
		}
	}	
	
	public void init(PortletConfig config) throws PortletException {
		super.init(config);
		normalView = config.getPortletContext().getRequestDispatcher(NORMAL_VIEW);
	}
	
	public void destroy() {
		normalView = null;
		super.destroy();
	}

	private void validate(ActionRequest request) {
		errorList = new HashMap<String, String>();
		
		String parameterTaskName = request.getParameter(PortletsUtils.PARAMETER_TASK_NAME);
		String parameterTaskDescription = request.getParameter(PortletsUtils.PARAMETER_TASK_DESCRIPTION);
		
		if (parameterTaskName != null && parameterTaskName.trim().equals("")) {
			errorList.put(PortletsUtils.ERROR_PARAMETER_TASK_NAME, PortletsUtils.ERROR_PARAMETER_TASK_NAME);
		} else {
			task.setName(parameterTaskName);
		}
		
		if (parameterTaskDescription != null && parameterTaskDescription.trim().equals("")) {			
			errorList.put(PortletsUtils.ERROR_PARAMETER_TASK_DESCRIPTION, PortletsUtils.ERROR_PARAMETER_TASK_DESCRIPTION);
		} else {
			task.setDescription(parameterTaskDescription);
		}
		
	}
	
}
